home *** CD-ROM | disk | FTP | other *** search
/ Amiga Tools 4 / Amiga Tools 4.iso / tools / netzwerk / tcp-ip / ipdial_v1.9 / buffer.c next >
C/C++ Source or Header  |  1996-02-26  |  5KB  |  266 lines

  1. /**
  2. ***  IPDial     Script program for initializing a SLIP connection
  3. ***  Copyright  (C)   1994    Jochen Wiedmann
  4. ***
  5. ***  This program is free software; you can redistribute it and/or modify
  6. ***  it under the terms of the GNU General Public License as published by
  7. ***  the Free Software Foundation; either version 2 of the License, or
  8. ***  (at your option) any later version.
  9. ***
  10. ***  This program is distributed in the hope that it will be useful,
  11. ***  but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ***  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. ***  GNU General Public License for more details.
  14. ***
  15. ***  You should have received a copy of the GNU General Public License
  16. ***  along with this program; if not, write to the Free Software
  17. ***  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. ***
  19. ***
  20. ***
  21. ***  This file implements dynamically growing buffers.
  22. ***
  23. ***
  24. ***  Computer: Amiga 1200                       Compiler: Dice 3.01
  25. ***
  26. ***  Author:    Jochen Wiedmann
  27. ***             Am Eisteich 9
  28. ***             72555 Metzingen
  29. ***             Germany
  30. ***
  31. ***             Phone: (+0049) 7123 / 14881
  32. ***             Internet: wiedmann@neckar-alb.de
  33. **/
  34.  
  35.  
  36.  
  37.  
  38.  
  39. /**
  40. ***  Include files
  41. **/
  42. #ifndef IPDIAL_H
  43. #include "IPDial.h"
  44. #endif
  45. #include <ctype.h>
  46.  
  47.  
  48. #ifndef MAX
  49. #define MAX(a,b) (((a)>(b))?(a):(b))
  50. #endif
  51.  
  52.  
  53.  
  54.  
  55.  
  56. /**
  57. ***  Type definitions
  58. **/
  59. typedef struct
  60. { STRPTR buf;
  61.   ULONG size;
  62.   ULONG filled;
  63. } Buffer;
  64.  
  65.  
  66.  
  67.  
  68.  
  69. /*** BufferCreate() function
  70. ***
  71. ***  Creates a new buffer.
  72. **/
  73. APTR BufferCreate(VOID)
  74.  
  75. { Buffer *buf;
  76.  
  77.   if ((buf = malloc(sizeof(*buf))))
  78.   { if ((buf->buf = malloc(2048)))
  79.     { buf->size = 2048;
  80.       BufferClear(buf);
  81.     }
  82.     else
  83.     { free(buf);
  84.       buf = NULL;
  85.     }
  86.   }
  87.   return(buf);
  88. }
  89.  
  90.  
  91.  
  92.  
  93.  
  94. /*** BufferClear() function
  95. ***
  96. ***  This function clears a buffer.
  97. **/
  98. VOID BufferClear(APTR buf)
  99.  
  100. { ((Buffer *) buf)->filled = 0;
  101.   ((Buffer *) buf)->buf[0] = '\0';
  102. }
  103.  
  104.  
  105.  
  106.  
  107.  
  108. /**
  109. ***  This function adds the given number of bytes to a buffer.
  110. **/
  111. VOID BufferExtend(APTR buffer, const UBYTE* addbuffer, ULONG addsize)
  112.  
  113. { Buffer *buf = buffer;
  114.   ULONG newfilled = buf->filled + addsize;
  115.   extern ULONG EchoMode;
  116.  
  117.   /**
  118.   ***  Allocate a new buffer, if needed.
  119.   **/
  120.   if (newfilled+1 > buf->size)
  121.   { buf->size = MAX(newfilled+1, buf->size)*2;
  122.  
  123.     if (!(buf->buf = realloc(buf->buf, buf->size)))
  124.     { perror("malloc");
  125.       exit(10);
  126.     }
  127.   }
  128.  
  129.   /**
  130.   ***  We are now guaranteed, that the buffer is big enough.
  131.   ***  Copy data and ensure, that we get a valid C string.
  132.   **/
  133.   memcpy(buf->buf + buf->filled, addbuffer, addsize);
  134.   buf->filled += addsize;
  135.   buf->buf[buf->filled] = '\0';
  136. }
  137.  
  138.  
  139.  
  140.  
  141.  
  142. /*** BufferCheck() function
  143. ***
  144. ***  Checks, if a buffer contains one of the given strings.
  145. ***  If this is the case, the strings number (beginning with
  146. ***  0) will be returned, -1 otherwise.
  147. **/
  148. LONG BufferCheck(APTR buffer, STRPTR *args)
  149.  
  150. { Buffer *buf = buffer;
  151.   LONG i;
  152.  
  153.   if (buf->filled)
  154.   { for (i = 0;  *args;  ++i, ++args)
  155.     { if (strstr((char *) buf->buf, (char *)  *args))
  156.       { return(i);
  157.       }
  158.     }
  159.   }
  160.   return(-1);
  161. }
  162.  
  163.  
  164.  
  165.  
  166.  
  167. /*** BufferBuffer() function
  168. ***
  169. ***  Returns the string associated to a buffer.
  170. **/
  171. STRPTR BufferBuffer(APTR buf)
  172.  
  173. { return(((Buffer *) buf)->buf);
  174. }
  175.  
  176.  
  177. /*** BufferExpand() function
  178. ***
  179. ***  Replaces patterns like $Var or ${Var} with the value
  180. ***  of the environment variable Var.
  181. ***
  182. ***  Uses an internal buffer; this is static and must not
  183. ***  be used after the next call to BufferExpand.
  184. **/
  185. STRPTR BufferExpand(STRPTR str, ULONG len)
  186.  
  187. { STATIC Buffer *expandBuf = NULL;
  188.  
  189.   /**
  190.   ***  Be sure, that the buffer is valid.
  191.   **/
  192.   if (!expandBuf  &&  !(expandBuf = BufferCreate()))
  193.   { perror("malloc");
  194.     exit(10);
  195.   }
  196.   BufferClear(expandBuf);
  197.  
  198.   /**
  199.   ***  Parse the string.
  200.   **/
  201.   while(len)
  202.   { STRPTR ptr = str;
  203.  
  204.     while (len  &&  *ptr != '$')
  205.     { --len;
  206.       ++ptr;
  207.     }
  208.     BufferExtend(expandBuf, str, ptr-str);
  209.     str = ptr;
  210.  
  211.     if (len)    /*  Found '$'   */
  212.     { --len;    /*  Skip it     */
  213.       ++str;
  214.  
  215.       if (len)
  216.       { if (*str == '$')    /*  Replace "$$" with "$"   */
  217.     { BufferExtend(expandBuf, (STRPTR) "$", 1);
  218.       --len;
  219.       ++str;
  220.     }
  221.     else
  222.     { int varNameLen;
  223.       char *varName;
  224.       char *varVal;
  225.  
  226.       if (*str == '{')  /*  Replace "${Var}" with value of  */
  227.       { --len;          /*  of environment variable Var.    */
  228.         ++str;
  229.         varName = (char*) str;
  230.         while (len  &&  *str != '}')
  231.         { --len;
  232.           ++str;
  233.         }
  234.         varNameLen = (char *) str - varName;
  235.         if (len)    /*  Skip '}'    */
  236.         { --len;
  237.           ++str;
  238.         }
  239.       }
  240.       else                      /*  Replace "$Var" with value of    */
  241.       { varName = (char*) str;  /*  environment variable Var.       */
  242.         while (len  &&  isalpha(*str))
  243.         { --len;
  244.           ++str;
  245.         }
  246.         varNameLen = (char *) str - varName;
  247.       }
  248.  
  249.       if (!(ptr = malloc(varNameLen+1)))
  250.       { perror("malloc");
  251.         exit(10);
  252.       }
  253.       strncpy((char*) ptr, varName, varNameLen);
  254.       ptr[varNameLen] = '\0';
  255.       if ((varVal = getenv((char*) ptr)))
  256.       { BufferExtend(expandBuf, (STRPTR) varVal, strlen(varVal));
  257.       }
  258.       free(ptr);
  259.     }
  260.       }
  261.     }
  262.   }
  263.  
  264.   return(BufferBuffer(expandBuf));
  265. }
  266.